﻿#ifndef COMMONTYPE_H
#define COMMONTYPE_H
#include <QtGui>
#include <QTextStream>
#include <QTableWidget>
#include <QTextEdit>
#include <QApplication>
#include <QDesktopWidget>
#include <QtMessageHandler>
#include "logwin.h"
extern LogWin *dlgLog;

//#define SHOW_DEBUG_WIN
#define APPDIR QApplication::applicationDirPath()
#define myDebug      qDebug()<< __FILE__ << __LINE__ << ":"
//界面端口：5100,设备端口：5200
#define UI_PORT 5100
#define DEVICE_PORT 41000
#define DEVICE_IP "192.168.1.19"
//电能表基本信息
typedef struct{
    int jxfs; //3 三线，4 四线
    QString strZCBH;
    QString strBCDY;
    QString strBCDL;
    QString strYHMC;
    QString strYHDZ;
    QString strXLMC;
    QString strXLBH;
    QString strCZMC;
    QString strJD;
    int dwV,dwI;//档位
    // 下面的参数界面用
    int idxU;   // 额定电压索引
    int idxI;   // 标定电流索引
    int bbU;    // 电压变比索引
    int bbI;    // 电流变比索引

    QString e_ding_dian_ya;         // 额定电压
    QString biao_ding_dian_liu;     // 标定电流（组合）
    QString Ib, Imax; //标定电流、最大电流
    double BianBiU;
    double BianBiI;
    int meter_type;//0 9-1,1 9-2
    float szUrange;
    float szIrange;
    bool bShowModel;
}Meter_BaseInfo;

typedef struct
{
    char head[4];
    uint cmd;
    uint len;
}PackHead;

/*****************相电压转换为线电压所需结构定义和函数声明****************/
typedef float FP32;

//直角坐标系 x，y， 实部，虚部
typedef struct
{
    FP32    re;
    FP32    im;
}Rect;

//极坐标系  幅值 角度
typedef struct
{
    FP32    mag;
    FP32    ang;
}Polar;
//一个点或向量的两种坐标表达
typedef struct
{
      Rect    rectvalue;
      Polar   polarvalue;
}Vector;

//三相电压的向量和序分量表示法
typedef struct
{
      Vector  Pos;
      Vector  Neg;
      Vector  Zero;
      Vector  A;
      Vector  B;
      Vector  C;
}ST_SEQ;

Rect RectAdd(Rect a, Rect b);
Rect RectSub(Rect a, Rect b);
Rect RectMult(Rect a, Rect b);
Rect RectDiv(Rect a, Rect b);
Rect ConvertToRect(Polar smpst);
Polar ConvertToPolar(Rect mulrslt);
void ConvertToSeq(ST_SEQ * pSEQ);
void ConvertToDrt(ST_SEQ * pSEQ);
void ConvertToLV(ST_SEQ * pUSeq, ST_SEQ *pLSeq);
/*****************相电压转线电压定义完毕*************************/
extern Meter_BaseInfo szInfo;
extern QList<double> idxU;
extern QList<double> bbU;
extern QList<double> bbI;
extern QList<double> idxI;
extern double g_szU[3];//测量到的数字电压
extern double g_szI[3];//测量到的数字电流
extern double g_lvU[3];//测量到的模拟电压
extern double g_lvI[3];//测量到的模拟电流


double s2d(QString s,bool *ok=0);
double s2i(QString s,bool *ok=0);
float s2f(QString s);
QString i2s(int i);
QString i3s(int i);
QString d2sl(double d, int strlen=6);
QString u2s(unsigned int u);
QString f2s(float f, int fraction=4);
QString d2s(double d, int fraction=4);

QString splitNum_Unit(QString inStr, QString &strUnit);
void MySleep(int msec);
QString ByteArray2HexString(QByteArray &ba);
bool writeInit(QString group, QString user_key, QString user_value);
bool readInit(QString group, QString user_key, QString &user_value);
QString getCompileDate();
double glys2xj(QString glys);
double wchz(double yz, double jd);
QString getSubStr(QString strFrom,QString strBegin,QString strEnd);
QString getSubStr(QString strFrom,QString strBegin);
int bytesToInt(QByteArray bytes);
QByteArray  intToByte(int number);
float bytesToFloat(QByteArray bytes);
QByteArray  floatToByte(float number);
bool copyFileToPath(QString fromFile , QString toFile, bool coverFileIfExist);
bool copyDirectoryFiles(const QString &fromDir, const QString &toDir, bool coverFileIfExist);
int DeleteFileInDir(const QString dirPath,QString strBegin);
int GetSelectedRow(QTableWidget *table);
QString getCellValue(QTableWidget *table,int r, int c);
void setCellValue(QTableWidget *table, int r, int c, QString v, bool bOk=true);
void centerWin(QWidget *w);
//float geScaleFactor();
QString getHostIpAddress();
void getLangFromXml();
void setLangToXml();

int getRangeV(double d);
int getRangeA(double d);
int getVolRange(QString Vol);
int getCurRange(QString Cur);

//日志系统
void openLog(QTextEdit *txtLog=NULL);
void closeLog();
void customMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg);

#endif // COMMONTYPE_H
